package com.pan.hisystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pan.hisystem.pojo.RespBean;
import com.pan.hisystem.pojo.User;
import com.pan.hisystem.pojo.UserLoginParam;
import com.pan.hisystem.service.IUserRoleService;
import com.pan.hisystem.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.List;

@RestController
public class LoginController {

    @Autowired
    private IUserService userService;
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private IUserRoleService userRoleService;

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public RespBean Login(@RequestBody UserLoginParam userLoginParam, HttpServletRequest request){
        return userService.login(userLoginParam.getUsername(),userLoginParam.getPassword(),userLoginParam.getCode(),request);
    }

    @ApiOperation(value = "获取登录当前操作员")
    @GetMapping("/getUser")
    public User getUser(Principal principal){
        String name=principal.getName();
        User user =userService.getOne(new QueryWrapper<User>().eq("username",name));
        user.setPassword(null);
        user.setRoles(userService.getRoles(user.getId()));

        return user;
    }


    @ApiOperation(value = "退出登录")
    @GetMapping(value = "/logout")
    public RespBean Logout(){
        return RespBean.success("注销成功");
    }

}
